package com.xuliugen.security.web.controller.async;

import com.xuliugen.security.web.dto.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.concurrent.Callable;

/**
 * 使用Callable实现异步请求
 * Created by xuliugen on 2017/11/5.
 */
@RestController
@RequestMapping(value = "callable")
public class CallableController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 不适用于正式业务
     * @return
      * @throws Exception
     */
    @RequestMapping("/order")
    public Callable<User> order() throws Exception {
        logger.info("主线程开始");

        Callable<User> result = () -> {
            logger.info("副线程开始");
            Thread.sleep(1000);
            logger.info("副线程返回");
            return new User("1", "xuliugen", "123456", new Date());
        };
        logger.info("主线程返回");
        return result;
    }
}
